package com.myidis.servlet;

import com.auxiliary.SignalIndexCalculator;
import com.auxiliary.SignalLampCalculator;
import com.auxiliary.SignalLampTimeSequence;
import com.auxiliary.TimeSequence;
import com.myidis.entity.*;
import com.myidis.mapper.*;
import com.myidis.request.SLCalculatorReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class SignalIndexCalculatorServlet {
    @Autowired
    SignalQuotaManagementMapper signalQuotaManagementMapper;
    @Autowired
    SignalIndexResultMapper signalIndexResultMapper;
    @Autowired
    SignalLampResultMapper signalLampResultMapper;
    @Autowired
    SignalThresholdMapper signalThresholdMapper;
    @Autowired
    QuotaMapper quotaMapper;
    @Autowired
    QuotaDataMapper quotaDataMapper;

    public ArrayList<TimeSequence> calculate(SLCalculatorReq req) {
        ArrayList<TimeSequence> signalTimeSequenceList =  new ArrayList<TimeSequence>();
        ArrayList<FullSLResult> ts = signalLampResultMapper.getByCombination(req.getCombination());
        ArrayList<TimeSequence> a = new ArrayList<>();
        ArrayList<Double> d = new ArrayList<>();
        for(int j = 0; j < ts.size(); j++){
            for(int i = 0; i < ts.size(); i++){
                d.add(ts.get(i).getLamp());
            }
            a.get(j).setValueList(d);
        }

        signalTimeSequenceList.add(new SignalIndexCalculator().Calculate(a));


        String subject = req.getSubject();
        String startTime = req.getStartTime();
        String endTime = req.getEndTime();
        String springDay = req.getSpringDay();
        String combination = req.getCombination();
        String calculator = req.getCalculator();
        String calTime = req.getCalTime();
        System.out.println(subject+" "+combination);
        signalIndexResultMapper.insertList(new IndexResultList(0,subject,springDay,calTime,combination,calculator,startTime,endTime));


        for(int i = 0; i < signalTimeSequenceList.size(); i++){
            for(int j = 0; j < signalTimeSequenceList.get(i).getValueList().size(); j++ ){
                String time = signalTimeSequenceList.get(0).getDateList().get(j);
                double index = signalTimeSequenceList.get(i).getValueList().get(j);
                FullSIResult DataSql = new FullSIResult(subject,time,index);
                signalIndexResultMapper.insertDetail(DataSql);

            }



        }
        return signalTimeSequenceList;
    }

    public ArrayList<IndexResultList> all() {
        return signalIndexResultMapper.list();
    }

    public ArrayList<FullSIResult> getData(IndexResultList sea) {
        return signalIndexResultMapper.getData(sea);
    }

    public FullSIResult getInfo(int sea) {
        return signalIndexResultMapper.getInfo(sea);
    }

    public int delect(int del) {
        signalIndexResultMapper.deleteData(del);
        return signalIndexResultMapper.delete(del);
    }
}
